<?php

declare (strict_types=1);

namespace app\bbxw\controller;


use think\admin\Controller;
use think\admin\helper\QueryHelper;
use app\bbxw\model\PowerSource as PowerSourceMod;

/**
 * 陪玩管理
 * @class Package
 * @package app\bbxw\controller
 */
class PowerSource extends Controller
{

    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'customer';
    /**
     * 陪玩管理
     * @auth true
     * @menu true
     * @login true
     */
    public function index()
    {
        PowerSourceMod::mQuery()->layTable(function () {
            $this->title = '陪玩管理';
        }, function (QueryHelper $query) {
            $query->like('name')->order('id asc');
        });
    }

    /**
     * 图文列表数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _index_page_filter(array &$data)
    {

        foreach ($data as &$v){
            $v['grade_name'] = "普通";
            if($v['grade']){
                $v['grade_name'] = $this->app->db->name("power_grade")->where(['id'=>$v['grade']])->value('title');
            }
        }
    }


    /**
     * 添加陪玩
     * @auth true
     */
    public function add()
    {
        PowerSourceMod::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isPost()) {
            if(!isset($data['id'])){
                $map = ['name'=>$data['name']];
                if(PowerSourceMod::mk()->where($map)->count()>0){
                    $this->error("陪玩名称已经存在，请使用其它名称！");
                }
            }
        } else {
            $this->power_grade = $this->app->db->name("power_grade")->order('id asc')->select()->toArray();
        }

    }
    /**
     * 编辑陪玩
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        PowerSourceMod::mForm('form');
    }


    /**
     * 删除陪玩
     * auth true
     */
    public function remove()
    {
        PowerSourceMod::mDelete();
    }

    /**
     * 重置密码
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function resetPassword()
    {
        $id = $this->app->request->get('id');
        PowerSourceMod::mk()->where(['id'=>$id])->update(['password'=>md5('123123')]);
        $this->success('重置密码成功,新的密码为123123');
    }
    /**
     * 修改用户密码
     * @auth true
     */
    public function pass()
    {
        if ($this->request->isGet()) {

            PowerSourceMod::mForm('pass');
        } else {
            $data = $this->_vali([
                'id.require'                  => '用户ID不能为空！',
                'password.require'            => '登录密码不能为空！',
                'repassword.require'          => '重复密码不能为空！',
                'repassword.confirm:password' => '两次输入的密码不一致！',
            ]);
            $user = PowerSourceMod::mk()->findOrEmpty($data['id']);
            if ($user->isExists() && $user->save(['password' => md5($data['password'])])) {

                $this->success('密码修改成功，请使用新密码登录！', '');
            } else {
                $this->error('密码修改失败，请稍候再试！');
            }
        }
    }



}
